package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.PTCreateUserListAdapter
import com.hinnotek.amoeba.bean.old.AmeobaCustomerListBean
import com.hinnotek.amoeba.databinding.ActivityPtCreateUserProjectListBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.IntentUtils
import com.lee.common.utils.SharedPreUtils
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.PublicInterfaceViewModel
import com.lee.common.network.net.ObserverState
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class PTCreateUserListActivity : BaseActivity<ActivityPtCreateUserProjectListBinding>() {

    val viewModel by viewModels<PublicInterfaceViewModel>()

    private var mAdapter: PTCreateUserListAdapter? = null
    private var customDialog: CustomDialog? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setLeftVisible(true)
        setTitle("客户")

        setContentView(R.layout.activity_pt_create_user_project_list)
    }

    override fun initVew() {
        bindingView.superButtonCreate.text = "创建客户"
        val manager = GridLayoutManager(mContext, 3)
        manager.orientation = RecyclerView.VERTICAL
        bindingView.recyclerView.layoutManager = manager
        bindingView.recyclerView.adapter = PTCreateUserListAdapter().also { mAdapter = it }
        bindingView.superButtonCreate.setOnClickListener {
            IntentUtils.startActivityForResult(
                this@PTCreateUserListActivity,
                PTCreateUserActivity::class.java,
                null,
                1
            )
        }
        mAdapter?.setOnItemClickListener(OnItemClickListener { adapter, view, position ->
            val amoebaCustomerData: AmeobaCustomerListBean =
                adapter.getItem(position) as AmeobaCustomerListBean
            val bundle = Bundle()
            bundle.putInt("customerId", amoebaCustomerData.customerId)
            bundle.putString("title", amoebaCustomerData.customerName)
            IntentUtils.startActivity(mContext, PTProjectListActivity::class.java, bundle)
        })
        customDialog = CustomDialog(mContext, "正在加载...")
        customDialog?.show()
    }

    override fun onResume() {
        super.onResume()
        getAmoebaCustomer()
    }

    override fun setData() {}

    private fun getAmoebaCustomer() {
        val orgId = SharedPreUtils.getInteger("orgId", 0)

        viewModel.getAmibaCustomerListByOrgId(orgId)
            .observe(this, object : ObserverState<MutableList<AmeobaCustomerListBean>>() {
                override fun onDataChange(data: MutableList<AmeobaCustomerListBean>) {
                    mAdapter?.setList(data)
                    super.onDataChange(data)
                }

                override fun onError(msg: String?, code: Int) {
                    ToastUtils.showShort(mContext, msg)
                    customDialog?.dismiss()
                    super.onError(msg, code)
                }
            })
    }
}